package com.lancoo.answer.view.xgSaResultView.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.lancoo.answer.databinding.EvXgAdapterAnswersheetParentBinding
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.model.bean.XgSaBean
import com.lancoo.answer.util.DensityUtils
import com.lancoo.answer.view.base.XBaseRecyclerAdapter
import com.lancoo.answer.view.base.XBaseRecyclerHolder

class XgSaResultAnswerSheetParentAdapter(
    val context: Context,
    val cognitiveBean: CognitiveBean
) : XBaseRecyclerAdapter<EvXgAdapterAnswersheetParentBinding>() {

    override fun onBindingView(viewGroup: ViewGroup?): EvXgAdapterAnswersheetParentBinding {
        return EvXgAdapterAnswersheetParentBinding.inflate(
            LayoutInflater.from(
                viewGroup!!.context
            ), viewGroup, false
        )
    }

    override fun onBindingData(
        holder: XBaseRecyclerHolder<EvXgAdapterAnswersheetParentBinding>,
        position: Int
    ) {
        holder.viewBinding.TvTypeInfor.visibility = View.GONE

        var sortIndex =
            cognitiveBean.TypeList!![0].QuesList!![0].ChildList!![0].ItemList!![0].SortIndex
        if(sortIndex!!.contains("-")){
            holder.viewBinding.RvAnswerSheetChild.layoutManager = GridLayoutManager(context, 1)
        }else {
            if (DensityUtils.isPad(context)) {
                holder.viewBinding.RvAnswerSheetChild.layoutManager = GridLayoutManager(context, 10)
            } else {
                holder.viewBinding.RvAnswerSheetChild.layoutManager = GridLayoutManager(context, 7)
            }
        }
        val params: LinearLayout.LayoutParams =
            holder.viewBinding.RvAnswerSheetChild.layoutParams as LinearLayout.LayoutParams
        params.setMargins(DensityUtils.dp2px(context,0f), 0, DensityUtils.dp2px(context,0f), 0)
        holder.viewBinding.RvAnswerSheetChild.layoutParams = params
        val adapterChild = XgSaResultAnswerSheetChildAdapter(
            context, cognitiveBean
        )
        holder.viewBinding.RvAnswerSheetChild.adapter = adapterChild
        adapterChild.setClickChildListener(object :
            XgSaResultAnswerSheetChildAdapter.OnClickChildListener {
            override fun onClick(sortIndex: String) {
                clickParentListener!!.onClick(sortIndex)
            }

        })
    }

    override fun getItemCount(): Int {
        return 1
    }

    interface OnClickParentListener {
        fun onClick(sortIndex: String)
    }

    private var clickParentListener: OnClickParentListener? = null

    fun setClickParentListener(clickParentListener: OnClickParentListener?) {
        this.clickParentListener = clickParentListener
    }
}